// import axios from 'axios';

// axios.interceptors.response.use(function (response) {
//   console.log(response, '0');
//   // 对响应数据做点什么
//   return response;
// }, function (error) {
//   // 对响应错误做点什么
//   return Promise.reject(error);
// });

// axios.interceptors.response.use(function (response) {
//   console.log(response, '1');
//   // 对响应数据做点什么
//   return response;
// }, function (error) {
//   // 对响应错误做点什么
//   return Promise.reject(error);
// });

import { systemShowLoading, systemHideLoading } from '../actions/globalActions.js';

export default store => next => action => {
  const { dispatch, getState } = store;
  const { promise, ...rest } = action;
  if (!promise) {
    return next(action);
  }
  /*请求时回调方法*/
  const onSuccessed = result => {
    dispatch(systemHideLoading());
    next({
      ...rest,
      result,
    });
  }
  /*请求失败时的回调方法*/
  const onFailed = error => {
    dispatch(systemHideLoading());
    next({
      ...rest,
      error
    });
  }

  dispatch(systemShowLoading());

  return promise().then(onSuccessed).catch(onFailed);
}